//支付回调信息，用于在线支付的回调
(function() {
	var db = require("../db/index");
	var config = require("../_include/config");
	var md5 = require("../_include/md5");
	var db_table_name = config.dbname.payAch;
	var db_id_prefix = config.dbIdPrefix.payAch;


	//构造函数
	var _con = function(user_id, ach_id) {
		var self = this;
		//ID编号
		self._id = db_id_prefix + ach_id;
		//支付者，和订单者可能不是同一个人，可以代付
		self.user_id = user_id;

		//订单
		var ach = AchCon.get(ach_id);
		if (!ach) {
			throwCusError("找不到指定订单");
		}
		self.ach_id = ach_id;
		//计算订单的价格
		ach.getCash();

		self.create_time = +new Date;

		self.security_key = md5.md5(Math.random().toString(36).substr(2) + (+new Date).toString(36));

		db.insert(db_table_name, self);
		console.log("生成一个支付回调信息：", self._id);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id) {
		var result = db.find_by_id(db_table_name, Id);
		// console.log(db.find_all(db_table_name))
		if (result) {
			result.__proto__ = _con.prototype;
		}
		return result;
	};
	_con.get2 = function(Id) {
		var result = db.find_one(db_table_name, {
			ach_id: Id
		});
		if (result) {
			result.__proto__ = _con.prototype;
		}
		// console.log(db.find_all(db_table_name))
		return result;
	};
	_con.build = function(user_id, ach_id) {
		var instance = _con.get(db_id_prefix + ach_id);
		if (!instance) {
			instance = new _con(user_id, ach_id)
		}
		return instance._id;
	}


	var AchCon = require("../ach/_con");
}());